<?php

namespace app\index\controller;

use think\Controller;
use think\Request;
use think\Session;
use think\Db;


class Send extends Controller
{

    Public function sendWeibo () {
        if (!request()->isPost()) {
            $this->error('页面不存在');
        }

        if (empty(request()->post('content'))){
            $this->error('内容不能为空');
        }

        $data = array(
            'content' => request()->post('content'),
            'time' => date('Y-m-d H:i:s',time()),
            'uid' => Session::get('id')
        );

        $mid = db()->table('wb_message')->insertGetId($data);
        if (!empty(request()->post('path'))) {
            $res = array(
                'path' => request()->post('path'),
                'mid' => $mid,
            );
            db()->table('wb_mpicture')->insertGetId($res);
        }
        //往相册表插入数据
        $uid = Session::get('id');
        if (!empty(request()->post('path'))){
            $path = array(
                'uid' => $uid,
                'name' => request()->post('path'),
            );
            Db::table('wb_photofolder')->insert($path);
        }

        //获取类型
        $data = array(
            'sid' => request()->post('style'),
            'mid'=>$mid
        );
        db()->table('wb_style_message')->insert($data);

        //获取话题名字
        $content = request()->post('content');
        $preg = '/\#(\S+?)\#/is';
        preg_match($preg, $content, $arr);
        //查询话题是否存在
        if (!empty($arr)){
            $res = Db::field('id,num')
                ->table(['wb_talk'])
                ->where('name','=',$arr[1])
                ->find();

            if ($res){
                //如果话题存在,存入话题微博关系表
                $tm = array(
                    'mid' => $mid,
                    'tid' => $res['id']
                );
                db()->table('wb_talk_message')->insert($tm);
                //改变话题表消息数量
                $num = $res['num']+1;

                //判断话题消息数量是否大于100
                if ($num >= 100){
                    $data = array(
                        'num'=>$num,
                        'hot'=>2
                    );
                    $result =  Db::table('wb_talk')->where('name', $arr[1])->update($data);

                }else{
                    $data = array(
                        'num'=>$num,
                    );
                    $result =  Db::table('wb_talk')->where('name', $arr[1])->update($data);
                }
            }else{
                //如果话题不存在,创建新话题
                $talk = array(
                    'name' => $arr[1],
                    'num' => 1,
                );
                $tid = db()->table('wb_talk')->insertGetId($talk);
                $tm = array(
                    'tid' => $tid,
                    'mid' => $mid
                );
                db()->table('wb_talk_message')->insert($tm);

            }

        }
        $this->success('发布成功');

    }

    /**
     * 转发微博
     */
    Public function turn ()
    {
        if (!request()->isAjax()) {
            $this->error('页面不存在');
        }

        $id = Session::get('id');
        if (empty($id)){
            return $data = false;
        }

        //获取数据
        $data = request()->post();
        //得到原微博id
        $src_id = $data['src_id'];
        //原微博类型
        $res = Db::field('sid')
            ->table(['wb_style_message'])
            ->where('mid = '.$src_id)
            ->find();

        if ($res['sid']){
            $res['mid'] = $src_id;
//            halt($res);
            db()->table('wb_style_message')->insert($res);
        }
        //获取原微博话题
        $res = Db::field('tid')
            ->table(['wb_talk_message'])
            ->where('mid = '.$src_id)
            ->find();
        //判断话题是否存在
        if ($res['tid']){
            $res['mid'] = $src_id;
            db()->table('wb_talk_message')->insert($res);
        }

        $data['uid'] = Session::get('id');
        $data['time'] = date('Y-m-d H:i:s');
        $mid = db()->table('wb_message')->insertGetId($data);
//        判断是否转发成功
        if ($mid){
            $res = Db::field('*')
                ->table(['wb_message'])
                ->where('id = '.$src_id)
                ->find();
            $res['transpondnum'] = $res['transpondnum'] + 1;
            $result =  Db::table('wb_message')->where('id',$src_id )->update($res);

        }
        $data = array(
            'mid' => $mid,
            'old_id' => $src_id,
            'res' => $res['transpondnum']
        );
        return $data;
    }

    // 上传缩略图
    public function uploadImg()
    {

        if(request()->isAjax()) {

            $file = request()->file('file');

            // 移动到框架应用根目录/public/uploads/ 目录下
            $info = $file->move(ROOT_PATH . 'public' . DS . 'upload');
            if ($info) {
                $src = '/upload' . '/' . date('Ymd') . '/' . $info->getFilename();

                return json(msg(0, ['src' => $src], ''));
            } else {
                // 上传失败获取错误信息
                return json(msg(-1, '', $file->getError()));
            }
        }
    }

//微博点赞处理
    public function agree($id)
    {
        if (!Request::instance()->isAjax()) {
            $this->error('你四不四迷路了!', '/');
            return;
        }
        $uid = Session::get('id');

        if ($uid == NULL) {
            $info[0] = 2;
            return $info;
        }

        $list = Db::field('mid,uid')
            ->table(['wb_agree'])
            ->where('mid = ' . $id . ' and uid')
            ->find();

        if ($list) {
            $info[0] = 0;
            return $info;
        } else {

            $data = array(
                'mid' => $id,
                'uid' => Session::get('id')
            );
            $res = db()->table('wb_agree')->insert($data);
            $arr = Db::field('*')
                ->table(['wb_message'])
                ->where('id = ' . $id)
                ->find();
            $arr['agreenum'] = $arr['agreenum'] + 1;
            if ($arr['agreenum'] > 99) {
                $arr['hot'] = 2;
                $result = Db::table('wb_message')->where('id', $id)->update($arr);
            } else {
                $result = Db::table('wb_message')->where('id', $id)->update($arr);
            }
//        halt($result);
            if ($result) {
                $info[0] = 1;

                $info[1] = $arr['agreenum'];
                return $info;
            }
        }



    }







}

